package com.sitanyun.merchant.guide.adapter;

import android.content.Context;
import android.os.CountDownTimer;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sitanyun.merchant.guide.R;
import com.sitanyun.merchant.guide.bean.ScenceHomeBean;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by jzl
 * on 2021/12/24.
 * 精品场景列表
 */
public class ScenHomeAdapter extends BaseQuickAdapter<ScenceHomeBean.DataBean.ListBean, BaseViewHolder> {

    private  Date one;
    private  Date two;
    private  long day = 0;
    private long hour = 0;
    private  long min = 0;
    private  long sec = 0;
    String days;
    //用于退出activity,避免countdown，造成资源浪费。
    private SparseArray<CountDownTimer> countDownMap;
    private CountDownTimer countDownTimer;

    public ScenHomeAdapter(Context context) {
        super(R.layout.scen_home);
        this.mContext = context;
        this.countDownMap = new SparseArray<>();
    }
    /**
     * 清空资源
     */
    public void cancelAllTimers() {
        if (countDownMap == null) {
            return;
        }
//        Log.e("TAG",  "size :  " + countDownMap.size());
        for (int i = 0,length = countDownMap.size(); i < length; i++) {
            CountDownTimer cdt = countDownMap.get(countDownMap.keyAt(i));
            if (cdt != null) {
                cdt.cancel();
            }
        }
    }

    /**
     * 赋值
     * @param helper
     * @param item
     */
    @Override
    protected void convert(BaseViewHolder helper, ScenceHomeBean.DataBean.ListBean item) {
     helper.setText(R.id.scen_name,item.getSceneName())
           .setText(R.id.scen_address,item.getSceneAddress())
           .setText(R.id.tv_fx,"分享"+item.getShareNum())
           .setText(R.id.tv_look,"浏览次数"+item.getBrowseNum())
           .setText(R.id.tv_tj,"同屏讲解"+item.getMeetingNum());
        ImageView scenimg = helper.getView(R.id.scen_img);


       if (item.getEndTime()==null){

       }else {
           DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
           try {
               //1 当前时间 ，2到期时间
               Date curDate = new Date(System.currentTimeMillis());//获取当前时间
               String str = df.format(curDate);
              one = df.parse(str);
               two = df.parse(item.getEndTime());
               long time1 = one.getTime();
               long time2 = two.getTime();
               long diff ;
               if(time1 >time2) {
                   helper.setVisible(R.id.ll_two,true);
                   helper.setText(R.id.tv_msg,"请联系"+item.getSaleName()+"："+item.getSalePhone());
                   helper.setVisible(R.id.tv_endtime,false);

               } else {
                   diff = time2 - time1;
            helper.setVisible(R.id.tv_endtime,true);
           helper.setVisible(R.id.ll_two,false);
               countDownTimer = new CountDownTimer(diff, 1000) {
                       public void onTick(long millisUntilFinished) {
                           day = millisUntilFinished / (24 * 60 * 60 * 1000);
                           hour = (millisUntilFinished / (60 * 60 * 1000) - day * 24);
                           min = ((millisUntilFinished / (60 * 1000)) - day * 24 * 60 - hour * 60);
                           sec = (millisUntilFinished/1000-day*24*60*60-hour*60*60-min*60);
                       String  s =(hour<10?("0"+hour):hour) + "时" + (min<10?("0"+min):min) + "分" + (sec<10?("0"+sec):sec+"秒");
                    if (day==0){
                       days=" ";
                   }else {
                       days=day+"天 ";
                   }
                    helper.setText(R.id.tv_endtime,"剩余："+ days + s );
                          // endtime.setText("剩余："+ days + s );
                       }
                       public void onFinish() {
//                           holder.timeTv.setText("00:00:00");
//                           holder.statusTv.setText(data.name + ":结束");
                       }
                   }.start();

              countDownMap.put(helper.getView(R.id.tv_endtime).hashCode(),countDownTimer);
               }
           } catch (ParseException e) {
               e.printStackTrace();
           }


       }
        Glide.with(mContext).load(item.getPicUrl()).into(scenimg);
        helper.addOnClickListener(R.id.wx_shar);
        helper.addOnClickListener(R.id.scen_img);
    ImageView imageView = helper.getView(R.id.shops);
      if (item.getMyStoreState()==1){
          imageView.setVisibility(View.VISIBLE);
      }else {
          imageView.setVisibility(View.GONE);
      }
    }
   //倒计时
   private boolean isRun = true;
    private void startRun() {


    }
    /**
     * 倒计时计算
     */
    private void computeTime() {
        sec --;
        if (sec < 0) {
            min--;
            sec = 59;
            if (min < 0) {
                min = 59;
                hour--;
                if (hour < 0) {
                    // 倒计时结束
                    hour = 23;
                    day--;
                }
            }
        }
    }
}
